﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web;
using System.IO;
using System.Drawing;
using System.Web.UI.WebControls;

namespace Comm
{
    /// <summary>
    /// 图片类
    /// </summary>
    public class Dispose_Image
    {
        /// <summary>
        /// 错误描述
        /// </summary>
        public string error = "";
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="filepath">原文件路径</param>
        /// <param name="directory ">文件保存目录</param>
        /// <returns>保存文件全路径(上传失败：返回空)</returns>
        public string upLoadImage(FileUpload fileupload, string directory)
        {
            if (fileupload == null || fileupload.PostedFile.FileName.Trim() == "")
            {
                error = "原路径不存在";
                return "";
            }
            string filepath = fileupload.PostedFile.FileName.Trim();
            if (!check_ExtendName(filepath))
            {
                error = "该文件不是图片格式的文件";
                return "";
            }
            if (!Directory.Exists(HttpContext.Current.Server.MapPath(directory))) Directory.CreateDirectory(HttpContext.Current.Server.MapPath(directory));
            directory += "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000) + Path.GetExtension(filepath);

            System.Drawing.Image img = null;
            try
            {
                img = System.Drawing.Image.FromStream(fileupload.PostedFile.InputStream);
                img.Save(HttpContext.Current.Server.MapPath(directory));
                img.Dispose();
                return directory;
            }
            catch
            {
                error = "该文件损坏或丢失";
                return "";
            }
        }


        /// <summary>
        /// 判断所选的文件是不是允许上传的文件
        /// </summary>
        /// <param name="extendName"></param>
        /// <returns></returns>
        private bool check_ExtendName(string filepath)
        {
            string extName = System.IO.Path.GetExtension(filepath);
            string[] extendNames = { ".jpg", ".jpge", ".gif", ".bmp", ".png" };
            foreach (string tempextName in extendNames)
            {
                if (tempextName.Trim().ToLower() == extName.Trim().ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="SavePath">原图的完整路径</param>
        /// <param name="sThumb">缩略图名的文件夹名</param>
        /// <param name="sSavePath">生成缩略图的路径</param>
        /// <param name="intThumbWidth">缩略图的宽度</param>
        /// <param name="intThumbHeight">缩略图的高度</param>
        /// <param name="sextendName">缩略图的后缀名</param>
        /// <returns></returns>
        public string upLoadThumbnail(string SavePath, string sSavePath, int intThumbWidth, int intThumbHeight)
        {
            System.Drawing.Image sourceImage = null;
            try
            {
                string name = System.IO.Path.GetFileName(SavePath);
                //加载原图
                sourceImage = System.Drawing.Image.FromFile(SavePath);

                //获取原图片的宽度、高度
                int width = sourceImage.Width;
                int height = sourceImage.Height;

                //缩略图的宽度、高度
                int sWidth = intThumbWidth;
                int sHeight = intThumbHeight;

                //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高) 
                //if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
                //{
                //    sWidth = intThumbWidth;
                //    sHeight = intThumbWidth * height / width;
                //}
                //else
                //{
                //    sWidth = intThumbHeight * width / height;
                //    sHeight = intThumbHeight;
                //}

                //设置缩略图生成的完整路径
                string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(string.Format("{0}/{1}", sSavePath, name));
                //创建一个缩略图大小的画板
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(sWidth, sHeight);
                //以缩略图的画板绘图
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                //设置成：高清、平滑
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //清除黑色
                g.Clear(System.Drawing.Color.Black);
                g.DrawImage(sourceImage, new System.Drawing.Rectangle(0, 0, sWidth, sHeight), new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.GraphicsUnit.Pixel);

                int lwidth = (sWidth - intThumbWidth) / 2;
                int bheight = (sHeight - intThumbHeight) / 2;
                g.DrawImage(bitmap, new System.Drawing.Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, System.Drawing.GraphicsUnit.Pixel);
                sourceImage.Dispose();
                g.Dispose();
                bitmap.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                bitmap.Dispose();

                return "";
            }
            catch (Exception ex)
            {
                return "缩略图生成失败";
            }
        }
    }
}
